package com.zlc.workflow.core.business.service.impl;

import com.zlc.common.core.business.model.vo.StoneReimbursementProcessVo;
import com.zlc.common.core.business.service.StoneReimbursementProcessService;
import com.zlc.common.util.AssertUtils;
import com.zlc.common.util.JsonHelper;
import com.zlc.workflow.core.business.service.ReimbursementService;
import com.zlc.workflow.core.instance.model.EventParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Map;

/**
 * 报销相关业务
 */
@Slf4j
@Service
public class ReimbursementServiceImpl implements ReimbursementService {

    @Autowired
    private StoneReimbursementProcessService stoneReimbursementProcessService;

    @Override
    public StoneReimbursementProcessVo findByProcessInstanceId(String processInstanceId) {
        return stoneReimbursementProcessService.queryByProcessInstanceId(processInstanceId);
    }

    @Override
    public void saveReimbursement(StoneReimbursementProcessVo reimburse) {
        AssertUtils.isTrue(!ObjectUtils.isEmpty(reimburse.getProcessInstanceId()), "流程实例id不可为空");
        if(ObjectUtils.isEmpty(reimburse.getApplyStatus())){
            reimburse.setApplyStatus("DRAFT");
        }
        stoneReimbursementProcessService.saveVo(reimburse);
    }

    @Override
    public void beforeStartProcess(EventParam param) {
        log.info("报销流程启动处理:{}", JsonHelper.toJson(param));
        String processInstanceId = param.getProcessInstanceId();
        StoneReimbursementProcessVo reimburse = stoneReimbursementProcessService.queryByProcessInstanceId(processInstanceId);
        if (!ObjectUtils.isEmpty(reimburse)) {
            reimburse.setApplyStatus("PROCESSING");
            stoneReimbursementProcessService.saveVo(reimburse);
        }
    }


    @Override
    public void finishProcess(EventParam param) {
        log.info("报销流程结束处理:{}", JsonHelper.toJson(param));
        String processInstanceId = param.getProcessInstanceId();
        StoneReimbursementProcessVo reimburse = stoneReimbursementProcessService.queryByProcessInstanceId(processInstanceId);
        if (!ObjectUtils.isEmpty(reimburse)) {
            reimburse.setApplyStatus("SUCCESS");
            stoneReimbursementProcessService.saveVo(reimburse);
        }

    }

    @Override
    public void terminationProcess(EventParam param) {
        log.info("报销流程终止处理:{}", JsonHelper.toJson(param));
        String processInstanceId = param.getProcessInstanceId();
        StoneReimbursementProcessVo reimburse = stoneReimbursementProcessService.queryByProcessInstanceId(processInstanceId);
        if (!ObjectUtils.isEmpty(reimburse)) {
            reimburse.setApplyStatus("TERMINATION");
            stoneReimbursementProcessService.saveVo(reimburse);
        }
    }

    @Override
    public void nodeArrive(EventParam param) {
        log.info("报销流程节点到达处理,到达节点:{},{}", param.getNodeName(), JsonHelper.toJson(param));
        String processInstanceId = param.getProcessInstanceId();
        StoneReimbursementProcessVo reimburse = stoneReimbursementProcessService.queryByProcessInstanceId(processInstanceId);
        if (!ObjectUtils.isEmpty(reimburse)) {
            Map<String, String> variableConfigs = param.getVariableConfigs();
            if (!ObjectUtils.isEmpty(variableConfigs) && !ObjectUtils.isEmpty(variableConfigs.get("applyStatus"))) {
                reimburse.setApplyStatus(variableConfigs.get("applyStatus"));
                stoneReimbursementProcessService.saveVo(reimburse);
            }
        }
    }

    @Override
    public void nodeEnd(EventParam param) {
        log.info("报销流程节点结束处理,结束节点:{},{}", param.getNodeName(), JsonHelper.toJson(param));
    }
}
